##=============================================================================
## Appendix Figure 8
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "hrbrthemes")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

num_col <- function(x){
  any(!is.character(x))
}

fam_1 <- raw_dat %>% 
  janitor::clean_names() %>%
  haven::zap_label() %>% 
  mutate_if(is.character, list(~na_if(., ""))) %>% 
  mutate_if(num_col, as.numeric) %>% 
  dplyr::select(v1, v133_1:v133_8) %>% 
  mutate(across(starts_with("v133_"), ~ifelse(.x == 1, 1, 0))) %>% 
  mutate(across(starts_with("v133_"), factor, levels = 0:1, 
                labels = c("No", "Yes"))) %>% 
  mutate(wave = 1) %>% 
  as.data.frame() %>% 
  rename(id = v1, 
         fam_1 = v133_1, 
         fam_2 = v133_2, 
         fam_3 = v133_3,
         fam_4 = v133_4, 
         fam_5 = v133_5, 
         fam_6 = v133_6, 
         fam_dk = v133_7,
         fam_ref = v133_8)

fam_2 <- raw_dat2 %>% 
  janitor::clean_names() %>%
  haven::zap_label() %>% 
  mutate_if(is.character, list(~na_if(., ""))) %>% 
  mutate_if(num_col, as.numeric) %>% 
  dplyr::select(v1, v187:v194) %>% 
  rename(id = v1) %>% 
  mutate(across(starts_with("v"), ~ifelse(.x == 1, 1, 0))) %>% 
  mutate(across(starts_with("v"), factor, levels = 0:1, 
                labels = c("No", "Yes"))) %>% 
  mutate(wave = 2) %>% 
  as.data.frame() %>% 
  rename(fam_1 = v187, 
         fam_2 = v188, 
         fam_3 = v189,
         fam_4 = v190, 
         fam_5 = v191, 
         fam_6 = v192, 
         fam_dk = v193,
         fam_ref = v194)

fam <- bind_rows(fam_1, fam_2) %>% 
  mutate(wave = factor(wave, levels = 1:2, labels = c("Wave 1", "Wave 2"))) %>% 
  as.data.frame()

rm(fam_1, fam_2)

fam <- fam %>% 
  as.data.frame() %>% 
  rename(
    "Services disrupted (electricity provision, water provision, garbage collection, etc.)" = fam_1,
    "Missile strikes or shelling of your city" = fam_2,
    "Missile strikes or shelling of your neighborhood" = fam_3,
    "Missile strikes or shelling of your home" = fam_4,
    "Looting or stealing of food or material goods" = fam_5,
    "Violence or other direct harm from Russian soldiers (shooting, beating, detention, etc.)" = fam_6,
    "Don't Know" = fam_dk,
    "Refused" = fam_ref)

mylikert <- likert::likert(fam[, 2:7], grouping = fam$wave)

plot(mylikert, colors = c("gray40", "gray20")) +
  scale_y_continuous(labels = likert:::abs_formatter, lim = c(-100, 100),
                     breaks = seq(-100, 100, 25)) +
  theme(text = element_text(size = 16))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================